package com.qa.controller;

import com.github.pagehelper.PageInfo;
import com.qa.common.Const;
import com.qa.common.ServerResponse;
import com.qa.pojo.Contact;
import com.qa.pojo.User;
import com.qa.service.IContactService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author
 * @Date 2020/3/13 21:41
 * @Desc
 **/
@Controller
public class ContactController {
    @Autowired
    private IContactService contactService;



    @PostMapping("contact/add")
    @ResponseBody
    public ServerResponse contactAdd(Contact contact, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(Const.CURRENT_USER);
        return contactService.add(contact, user);
    }

    @GetMapping("/cms/contact/listPage")
    public String userListPage() {
        return "contactList";
    }

    @GetMapping("/cms/contact/list")
    @ResponseBody
    public ServerResponse userList(@RequestParam(value = "userEmail", defaultValue = "") String userEmail,
                                   @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "limit", defaultValue = "10") Integer pageSize) {
        PageInfo<Contact> pageInfo = contactService.getByEmail(userEmail, pageNum, pageSize);
        return ServerResponse.createBySuccess("查询成功！", pageInfo.getList(), pageInfo.getTotal());
    }

    @GetMapping("/cms/hui/fu")
    public String huiFUPage(@RequestParam("id") Integer id, Model model) {
        Contact contact = contactService.getById(id);
        model.addAttribute("contact", contact);
        return "contactEditPage";
    }

    @PostMapping("/cms/liu/yan/hui/fu")
    @ResponseBody
    public ServerResponse liuYanHuiFu(Contact contact) {
        return null;
    }

    @GetMapping("/cms/contact/delete")
    @ResponseBody
    public ServerResponse delete(@RequestParam("id") Integer id) {
        return contactService.deleteById(id);
    }
}
